<template>
  <div v-loading="loading">
    <el-descriptions :column="1">
      <el-descriptions-item label="学号"
        >{{ detail?.user?.acc }}
      </el-descriptions-item>
      <el-descriptions-item label="姓名">
        {{ detail?.user?.name }}
      </el-descriptions-item>

      <el-descriptions-item label="角色">
        {{ detail?.user?.identity || "-" }}
      </el-descriptions-item>
      <el-descriptions-item label="项目名称">
        {{ detail?.projectName || "-" }}
      </el-descriptions-item>
      <el-descriptions-item label="第一阶段答辩得分:">
        {{ detail?.db_first || "-" }}
      </el-descriptions-item>
      <el-descriptions-item label="第二阶段答辩得分:">{{
        detail?.db_second || "-"
      }}</el-descriptions-item>
      <el-descriptions-item label="中期答辩得分:">{{
        detail?.db_mid || "-"
      }}</el-descriptions-item>
      <el-descriptions-item label="终期答辩得分:">{{
        detail?.db_terminal || "-"
      }}</el-descriptions-item>
      <el-descriptions-item label="个人最终得分:">{{
        detail?.db_final || "-"
      }}</el-descriptions-item>
      <!-- <el-descriptions-item label="组长">
              {{ detail?.proRequirement || "-" }}
            </el-descriptions-item> -->
    </el-descriptions>
    <h3 style="margin: 20px 0">日报管理</h3>
    <el-collapse v-model="activeLog" accordion>
      <template v-for="(item, index) of logManage" :key="index">
        <el-collapse-item :name="index">
          <template #title>
            <div class="flex-between w-full">
              <div>
                {{ `第${index + 1}周` }}
                <el-tag
                  v-if="!!item.wrote"
                  type="success"
                  style="margin-left: 10px"
                  >已完成</el-tag
                >
                <el-tag v-else type="info" style="margin-left: 10px"
                  >未完成</el-tag
                >
                <el-text v-if="!!item.weekScore" style="margin-left: 10px"
                  >(组长打分: {{ item.weekScore || 0 }})
                </el-text>
              </div>
              <el-button
                v-if="!!item.wrote"
                class="mr-10"
                type="primary"
                size="small"
                plain
                @click.stop="toMatrix(item)"
              >
                AI 分析
              </el-button>
            </div>
          </template>
          <el-timeline>
            <template v-for="(log, i) in item.dailyReport" :key="i">
              <el-timeline-item :timestamp="`${log.date}`" placement="top">
                <writeDaily
                  :date="log.date"
                  :content="log.report"
                  :edit="!item.weekScore"
                  @onEdit="editDaily($event, log)"
                />

                <div style="margin-top: 10px" v-if="log.report">
                  <div v-if="log.conclude">
                    <h4>AI 总结</h4>
                    <el-text class="flex align-center">
                      {{ log.conclude }}
                    </el-text>
                  </div>
                  <div class="flex align-center">
                    <el-text> 自评分： </el-text>
                    <span v-if="currentLogId === log.id">
                      <el-input-number
                        v-model="score"
                        :precision="1"
                        :step="0.1"
                        :max="10"
                      />
                      <el-button
                        style="margin-left: 5px"
                        link
                        @click.stop="submitScore(log)"
                      >
                        <el-icon><Select /></el-icon>
                      </el-button>
                    </span>
                    <span class="flex align-center" v-else>
                      {{ log.selfEval }}
                      <el-button
                        v-if="!item.weekScore"
                        @click.stop="selfRate(log)"
                        style="margin-left: 5px"
                        link
                      >
                        <el-icon><EditPen /></el-icon>
                      </el-button>
                    </span>
                  </div>
                </div>
              </el-timeline-item>
            </template>
          </el-timeline>
        </el-collapse-item>
      </template>
    </el-collapse>
    <MatrixDialog ref="MatrixDialogRef" />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, computed, watch } from "vue";
import api from "@/api";
import MatrixDialog from "./MatrixDialog.vue";
import useStore from "@/stores";
const { user } = useStore();

const userAcc = localStorage.getItem("userAcc") as string;

const MatrixDialogRef = ref();
const loading = ref(false);
const detail = ref<any>(null);
const activeLog = ref(0);
const logManage = ref<any>({});
const score = ref(0);
const currentLogId = ref("");

function submitScore(data: any) {
  const obj = {
    ...data,
    selfEval: score.value,
  };
  updateDaily(obj);
  score.value = 0;
  currentLogId.value = "";
}
function selfRate(data: any) {
  currentLogId.value = data.id;
}

function editDaily(content: string, data: any) {
  if (data?.id) {
    const obj = {
      ...data,
      report: content,
    };
    updateDaily(obj);
  } else {
    const obj = {
      acc: userAcc,
      date: data.date,
      report: content,
    };
    loading.value = true;
    api.dailyReport
      .saveDailyReport(obj)
      .then((res: any) => {
        if (res.success) {
          loading.value = false;
          refresh();
        }
      })
      .catch(() => {
        loading.value = false;
      });
  }
}

function updateDaily(data: any) {
  loading.value = true;
  api.dailyReport
    .updateDailyReport(data)
    .then((res: any) => {
      if (res.success) {
        loading.value = false;
        refresh();
      }
    })
    .catch(() => {
      loading.value = false;
    });
}

function toMatrix(data: any) {
  MatrixDialogRef.value.open(data);
}

/*
 * Static state
 * */
function getTableData(val: string) {
  loading.value = true;
  api.dailyReport
    .DailyReportByAcc(val)
    .then((res: any) => {
      if (res.success) {
        detail.value = res.data;
        logManage.value = Object.values(
          Object.keys(res.data).reduce((newData: any, key: string) => {
            if (key.includes("week")) {
              newData[key as string] = res.data[key];
            }
            return newData;
          }, {})
        );
        if (logManage.value.length === 0) {
          logManage.value.push({ dailyReport: [] });
        }
        loading.value = false;
      }
    })
    .catch(() => {
      loading.value = false;
    });
}

function refresh() {
  getTableData(userAcc);
}
onMounted(() => {
  getTableData(userAcc);
});
</script>
<style lang="scss" scope></style>
